package com.wmyg.antifake.controller;

import com.wmyg.antifake.entity.UserAddress;
import com.wmyg.antifake.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
 * @author 王卓逸
 * @program TODO
 * @description TODO
 * @create 2019-05-26
 */
@RestController
@RequestMapping(value = "/useraddress")
public class UserAddressController {

    @Autowired
    UserAddressService userAddressService;

    @GetMapping(value = "/useraddresslist")
    public List<UserAddress> userAddressList(Principal principal) {
        return userAddressService.findUserAddressList(principal.getName());
    }

    @PostMapping(value = "/useraddress")
    public Boolean insertUserAddress(UserAddress address, Principal principal) {
        return userAddressService.insertUserAddress(address, principal.getName());
    }

    @PutMapping(value = "/useraddress")
    public Boolean updateUserAddress(UserAddress address, Principal principal) {
        return userAddressService.updateUserAddress(address, principal.getName());
    }

    @PutMapping(value = "/default")
    public Boolean setDefault(Integer addressId, Principal principal) {
        return userAddressService.setDefault(addressId, principal.getName());
    }

    @GetMapping(value = "/default")
    public UserAddress getDefault(Principal principal) {
        return userAddressService.getDefault(principal.getName());
    }

    @DeleteMapping(value = "/default")
    public Boolean setNotDefault(Integer addressId) {
        return userAddressService.setNotDefault(addressId);
    }

}
